#include "stdio.h"
#include "app.h"
#include "display_lcd.h"
#include "rtc_clock.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_err.h"
#include "time.h"
#include "lvgl.h"
#include "ui.h"

#include "wifi.h"

#include "http.h"

#include "audio_player.h"
#include "adf_service.h"
#include "music.h"
#include "my_ui.h"

#include <string.h>

static const char *TAG = "APP";
static DisplayLCD Display;
static RTC_Clock RTCClock;

// const char *mp3_file_path = "http://192.168.1.5:8090/testaudio.mp3";
// const char *pcm_file_path = "http://192.168.1.5:8090/output.pcm";

// static struct tm timeinfo;



APP::APP()
{
}
APP::~APP()
{
}

void APP::Start()
{
    Display.init();
    Display.turnOn();
    RTCClock.init();
    RTCClock.set_time(); // 从rtc获取并设置系统时间

    xTaskCreate(main_loop, "main_loop", 4096, NULL, 5, NULL);
    xTaskCreate(main_longdelay_loop, "main_loop", 4096, NULL, 5, NULL);
    Wifi &wifi = Wifi::get_instance();
    // AUDIO_PLAYER &audio_player = AUDIO_PLAYER::get_instance();
    // audio_player.say_chinese("开始连接wifi");

    ADF_SERVICE& adf_service = ADF_SERVICE::get_instance();

    // adf_service.play_tts("adf初始化");


    wifi.connect("panogk", "12011201");
    while (wifi.is_connected() == false)
    {
        /* code */
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

    adf_service.play_tts("网络连接成功");
    vTaskDelay(pdMS_TO_TICKS(1000));
    Music& music = Music::get_instance();
    music.get_song_list("http://fxdp.panogk.cn/testsound/sound.php");
    



    // wifi.smartconfig_start();
    // ui_event_controller();

    // Wifi& wifi = Wifi::get_instance();
    // // const char *ssid = "ssid";
    // // const char *password = "password";
    // // wifi.connect(ssid, password);

    // wifi.smartconfig_start();
    // // wifi.connect();

    // while (wifi.is_connected() == false)
    // {
    //     /* code */
    //     vTaskDelay(pdMS_TO_TICKS(1000));
    // }

    //     vTaskDelay(pdMS_TO_TICKS(1000));

    // AUDIO_PLAYER& audio_player = AUDIO_PLAYER::get_instance();
    // audio_player.play_url(pcm_file_path);

    // vTaskDelay(pdMS_TO_TICKS(8000));
    // audio_player.stop();
}

void APP::Reboot()
{
}
void APP::main_loop(void *)
{
    ESP_LOGI(TAG, "main_loop start");
    lv_obj_t *clock_page = ui_home;
    lv_obj_t *seconds_pointer = ui_second;
    lv_obj_t *minutes_pointer = ui_minute;
    lv_obj_t *hours_pointer = ui_hour;
    static struct tm timeinfo_storage;
    static struct tm *timeinfo = &timeinfo_storage;
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
        // ESP_LOGI(TAG, "main_loop running");
        if (lv_scr_act() == clock_page)
        { // 判断是否存在时钟页面
            ESP_LOGI(TAG, "change home clock");

            time_t current_time = RTCClock.now_system_time();

            timeinfo = localtime(&current_time);
            int tm_sec = timeinfo->tm_sec;
            int tm_min = timeinfo->tm_min;
            int tm_hour = timeinfo->tm_hour;

            lv_img_set_angle(seconds_pointer, tm_sec * 10 * 6);
            // 每分钟6°
            lv_img_set_angle(minutes_pointer, tm_min * 10 * 6 + tm_sec * 10 * 6 / 60);
            // 每小时30°
            lv_img_set_angle(hours_pointer, tm_hour * 10 * 30 + tm_min * 10 * 30 / 60);
        }
    }
    vTaskDelete(NULL);
}
void APP::main_longdelay_loop(void *)
{
    ESP_LOGI(TAG, "main_longdelay_loop start");
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(10000));
        // ESP_LOGI(TAG, "main_loop running");
        RTCClock.set_time_RTC(RTCClock.now_system_time()); // 每隔10秒更新系统时间到rtc
    }
    vTaskDelete(NULL);
}

void APP::light_on_screen()
{
}

void APP::light_off_screen()
{
}

void APP::sr_start_listen()
{

    // 语音识别
}
void APP::sr_stop_listen()
{
}

void APP::tts_chinese_say(const char *text)
{

    // 说中文
}

void APP::record_sound()
{

    // 录制pcm音频
}

void APP::play_sound(uint16_t *pcm)
{
}
